package org.minbox.chapter.seata;

import com.alibaba.fescar.spring.annotation.GlobalTransactional;
import com.cloud.model.order.Order;
import lombok.extern.slf4j.Slf4j;
import org.minbox.chapter.seata.feign.AccountClient;
import org.minbox.chapter.seata.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 恒宇少年
 */
@RestController
@RequestMapping(value = "/order")
@Slf4j
public class OrderController {
    /**
     * 账户服务接口
     */
    @Autowired
    private AccountClient accountClient;

    /**
     * 订单业务逻辑
     */
    @Autowired
    private OrderService orderService;


    @GlobalTransactional
    public String submitOrder(
            @RequestParam("goodId") Integer goodId,
            @RequestParam("accountId") Integer accountId,
            @RequestParam("price") double price) {

        Order order = new Order();
        order.setAccountId(accountId);
        order.setGoodId(1);
        order.setPrice(price );
        boolean b = orderService.crateOrder(order);
        log.info("订单创建：{}",b);
        String deduction = accountClient.deduction(goodId, price);
        log.info("订单创建：{}",deduction);
        return "下单成功.";
    }
}
